package algorithm;


import io.OutputWriter;
import java.io.IOException;
import pac.SearchDatastructure;

public class CompleteSearch {
	
	private SearchDatastructure searcher;
	private OutputWriter outputter;
	
	public CompleteSearch(SearchDatastructure I, OutputWriter O) throws IOException {
		searcher = I;
		outputter = O;
	}
	
	public void doBruteForcePairwiseSearch() throws IOException {
		searcher.startTimeMs = System.currentTimeMillis( );				
		
		int lengthOfBitSet = 2*searcher.snpList.length;
		int progress = 1;
		
		for(int snp1=1; snp1<lengthOfBitSet; snp1++) {
			
			for(int snp2=0; snp2<snp1; snp2++) {
				
				//to check, search for snp1=13489 and snp2=1273 on shuffle.21and22.raw
				int order = 2;
				int[] variableArray = new int[] {snp1, snp2};
				
				int[][] noOfCaseCarriers = searcher.snpIndices.getObservedCaseCarriers(searcher.userParams, variableArray);
				int[][] noOfControlCarriers = searcher.snpIndices.getObservedControlCarriers(searcher.userParams, variableArray);
				double pvalueCaseControl = searcher.significance.TestCasesVsControls (order, noOfCaseCarriers, noOfControlCarriers );
				
				//Use the hard cut-off here. If controls are depleted below expectations, then we can boost the case-only analysis p-value.
				if ( pvalueCaseControl <= searcher.significance.getCaseControlThreshold ( order ) )
					outputter.WriteOutput ( variableArray, noOfCaseCarriers, 1, noOfControlCarriers, pvalueCaseControl );
				
			}	//end snp2 loops
			
			if( snp1 > progress * (double) lengthOfBitSet/100 ) {
				System.out.println("Finished " + Math.pow(progress, 2)/100 + "% in " + (System.currentTimeMillis( ) - searcher.startTimeMs)/1000 + " seconds.");
				progress++;
			}
			
		}	//end snp1 loop	

		outputter.Close();
		System.out.println("=========");
		System.out.println("Done.");
		System.out.println("Interactions found are listed in " + searcher.userParams.getStage2Output());
		System.out.println("=========");
		return;
		
	}
	
}
